package cn.gson.school.controller.jiaoxue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.QinShiDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.entity.jiaoxue.QinShiEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.mappers.jiaoxue.QinShiMappers;
import cn.gson.school.service.jiaoxue.StudentService;

@Controller
@RequestMapping("/qinshi")
public class QinshiControlller {
	@Autowired
	private QinShiMappers qinshiMapper;// 寝室

	@Autowired
	private QinShiDao qinshiDao;

	@Autowired
	private StudentDao studentDao;

	@Autowired
	private StudentService studentService;

	// 查询所有寝室
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> qinshi(QinShiEntity qinShiEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<QinShiEntity> list = qinshiMapper.qinshiList(qinShiEntity);
		PageInfo<QinShiEntity> info = new PageInfo<>(list);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	// 添加寝室
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> saveQinshi(@Valid QinShiEntity qinshi, HttpServletRequest request, Model model,
			BindingResult br) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		} else {
			String id = request.getParameter("id");
			String qinshiName = request.getParameter("qinshiName");// 获取界面寝室名称
			String name = request.getParameter("name");// 获取界面寝室名称
			QinShiEntity u = new QinShiEntity();
			if (id == null || "".equals(id)) {
//				System.out.println("编号是空的，要执行添加。。。。");
			}else {
//				System.out.println("编号不是空的，要执行编辑");
				// 根据id来将该id的信息查出来
				u = qinshiDao.findOne(Long.valueOf(id));
//				System.out.println("编号B："+id);
			}
			// 判断寝室名称是否为空
			if (qinshiName == null || "".equals(qinshiName)) {
				obj.put("message", "寝室名称不能为空！");
				return obj;
			} else {
				if (name == null || "".equals(name)) {
//					System.out.println("寝室长名称为空！");
//					System.out.println("成功添加了.。。");
				} else {
//					System.out.println("寝室长名称不是空的！");
					StudentEntity student = studentService.findByStudentName(name);
//					System.out.println("寝室长的信息：" + student);
					if (student == null) {
						obj.put("message", "没有该寝室长的信息！");
						return obj;
					} else {
//						System.out.println("找到了该寝室长的信息");
//						System.out.println("寝室长编号" + student.getStuId());
						u.setQinshizhang(student.getStuId());
					}
				}
			}
			u.setQinshiCount(1);
			u.setQinshiName(qinshiName);
			u.setQinshiState(1);
			qinshiDao.save(u);
			obj.put("success", true);
		}
		return obj;
	}

	// 删除寝室
	@GetMapping("delete/{id}")
	@ResponseBody
	// 指定使用jpa的事务管理器
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		QinShiEntity u = qinshiDao.findOne(id);
		if (id != null && u != null) {
			u.setDelete(0);
			qinshiDao.save(u);
			obj.put("success", true);
		} else {
			obj.put("message", "此用户不存在！");
		}
		return obj;
	}
}
